package com.xbongbong.saas.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.department.pojo.DepartmentUserPojo;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2021/07/07 15:41
 */
public class DepartmentFieldUtil {

    /**
     * 获取部门id信息
     * @param explainList
     * @param paasFormDataEntity
     * @return
     * @throws XbbException
     */
    public static Set<Long> getDepartmentIdList(List<FieldAttrEntity> explainList,  PaasFormDataEntity paasFormDataEntity) throws XbbException {
        Set<Long> departmentIdIn = new HashSet<>();
        JSONObject data = paasFormDataEntity.getData();
        if (CollectionUtils.isEmpty(explainList) && Objects.isNull(data) && !Objects.equals(0, paasFormDataEntity.getDepartmentId())) {
            return departmentIdIn;
        }
        Iterator<FieldAttrEntity> fieldAttrEntityIterator = explainList.iterator();
        while (fieldAttrEntityIterator.hasNext()) {
            FieldAttrEntity fieldAttrEntity = fieldAttrEntityIterator.next();
            String attr = fieldAttrEntity.getAttr();
            if ( Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPT.getType()) ) {
                Long userId = FastJsonHelper.getLongOrDefaultFromFormData(data, attr, null);
                if ( Objects.nonNull(userId) ) {
                    departmentIdIn.add(userId);
                }
            } else if ( Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPARTMENTID.getType()) && !Objects.equals(0, paasFormDataEntity.getDepartmentId()) ) {
                departmentIdIn.add(paasFormDataEntity.getDepartmentId());
            } else if ( Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPT_GROUP.getType()) ) {
                JSONArray departmentGroup = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, attr, new JSONArray());
                departmentGroup.forEach(str -> {
                    if ( Objects.nonNull(str) ) {
                        departmentIdIn.add(Long.valueOf(str.toString()));
                    }
                });
            } else if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
                List<? extends FieldAttrEntity> fieldList = fieldAttrEntity.getSubForm().getItems();
                if ( Objects.isNull(fieldList) ) {
                    continue;
                }
                JSONArray subFormDataArr = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, attr, new JSONArray());
                for ( FieldAttrEntity subField : fieldList ) {
                    String subFieldAttr = subField.getAttr();
                    for ( Object subFormDataObject : subFormDataArr ) {
                        if (! (subFormDataObject instanceof Map)) {
                            continue;
                        }
                        JSONObject subFormData = (JSONObject) subFormDataObject;
                        if (FieldTypeEnum.DEPT_GROUP.getType().equals(subField.getFieldType())) {
                            JSONArray jsonArrayUserGroup = FastJsonHelper.getJsonArrOrDefaultFromFormData(subFormData, subFieldAttr, new JSONArray());
                            jsonArrayUserGroup.forEach(str -> {
                                if ( Objects.nonNull(str) ) {
                                    departmentIdIn.add(Long.valueOf(str.toString()));
                                }
                            });
                        } else if ( FieldTypeEnum.DEPT.getType().equals(subField.getFieldType())) {
                            Long str = FastJsonHelper.getLongOrDefaultFromFormData(subFormData, subFieldAttr, null);
                            if ( Objects.nonNull(str) ) {
                                departmentIdIn.add(str);
                            }
                        }
                    }
                }
            }
        }
        return departmentIdIn;
    }

    public static Set<Long> getDepartmentIdByDataInfo(JSONObject data, List<FieldAttrEntity> explainList) throws XbbException {
        Set<Long> departmentIdIn = new HashSet<>();
        Iterator<FieldAttrEntity> fieldAttrEntityIterator = explainList.iterator();
        while (fieldAttrEntityIterator.hasNext()) {
            FieldAttrEntity fieldAttrEntity = fieldAttrEntityIterator.next();
            String attr = fieldAttrEntity.getAttr();
            if ( Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPT.getType()) ) {
                Long departmentId = FastJsonHelper.getLongOrDefaultFromFormData(data, attr, null);
                if ( Objects.nonNull(departmentId) ) {
                    departmentIdIn.add(departmentId);
                }
            } else if ( Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPT_GROUP.getType()) ) {
                JSONArray departmentGroup = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, attr, new JSONArray());
                departmentGroup.forEach(str -> {
                    if ( Objects.nonNull(str) ) {
                        departmentIdIn.add(Long.valueOf(str.toString()));
                    }
                });
            }else if(Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPARTMENTID.getType())){
                departmentIdIn.add(Long.valueOf(data.getString(attr)));
            } else if (ExplainUtil.isSubForm(fieldAttrEntity.getFieldType())) {
                List<? extends FieldAttrEntity> fieldList = fieldAttrEntity.getSubForm().getItems();
                if ( Objects.isNull(fieldList) ) {
                    continue;
                }
                JSONArray subFormDataArr = FastJsonHelper.getJsonArrOrDefaultFromFormData(data, attr, new JSONArray());
                for ( FieldAttrEntity subField : fieldList ) {
                    String subFieldAttr = subField.getAttr();
                    for ( Object subFormDataObject : subFormDataArr ) {
                        if (! (subFormDataObject instanceof Map)) {
                            continue;
                        }
                        JSONObject subFormData = (JSONObject) subFormDataObject;
                        if (FieldTypeEnum.DEPT_GROUP.getType().equals(subField.getFieldType())) {
                            JSONArray jsonArrayUserGroup = FastJsonHelper.getJsonArrOrDefaultFromFormData(subFormData, subFieldAttr, new JSONArray());
                            jsonArrayUserGroup.forEach(str -> {
                                if ( Objects.nonNull(str) ) {
                                    departmentIdIn.add(Long.valueOf(str.toString()));
                                }
                            });
                        } else if ( FieldTypeEnum.DEPT.getType().equals(subField.getFieldType())) {
                            Long str = FastJsonHelper.getLongOrDefaultFromFormData(subFormData, subFieldAttr, null);
                            if ( Objects.nonNull(str) ) {
                                departmentIdIn.add(str);
                            }
                        }
                    }
                }
            }
        }
        return departmentIdIn;
    }
}
